package cn.itcast.bos.service.take_delivery.impl;

import cn.itcast.bos.dao.base.PromotionRepository;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Transient;
import java.util.Date;


@Service
@Transactional
public class PromotionServiceImpl implements PromotionService {
    @Autowired
    private PromotionRepository promotionRepository;
    @Override
    public void save(Promotion model) {
        promotionRepository.save(model);
    }

    @Override
    public Page<Promotion> findByPage(PageRequest request) {
        Page<Promotion> all = promotionRepository.findAll(request);
        return all;
    }

    @Override
    public PageBean<Promotion> findPage(int page, int rows) {
        PageRequest request = new PageRequest(page-1, rows);
        Page<Promotion> all = promotionRepository.findAll(request);
        PageBean<Promotion> bean = new PageBean<>();
        bean.setTotalCount(all.getTotalElements());
        bean.setPageData(all.getContent());
        return bean;
    }

    @Override
    public Promotion findById(Integer id) {
        Promotion promotion = promotionRepository.findOne(id);
        return promotion;
    }

    @Override
    public void updateStatus(Date date) {
        promotionRepository.updateStatus(date);
    }
}
